package com.itrip.itripsearch.controller;


import com.itrip.common.DtoUtil;
import com.itrip.common.EmptyUtils;
import com.itrip.common.Page;
import com.itrip.common.SolrUtils;
import com.itrip.dto.Dto;
import com.itrip.vo.hotel.ItripHotelVO;
import com.itrip.vo.hotel.SearchHotCityVO;
import com.itrip.vo.hotel.SearchHotelVO;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/search/api/hotellist")
public class SearchController {

    @PostMapping("/searchItripHotelPage")
    public Dto searchItripHotelPage(@RequestBody SearchHotelVO searchHotelVO){
        //创建连接
        SolrServer solrServer = SolrUtils.solrServer;
        //创建查询对象
        SolrQuery query = new SolrQuery();
        StringBuilder sb = new StringBuilder();
        //目的地查询  AND (tradingAreaIds:*3669* OR tradingAreaIds:*3662* OR tradingAreaIds:*3664* OR tradingAreaIds:*3665*)
        sb.append("destination:\""+searchHotelVO.getDestination()+"\"");
        //位置查询
        //AND (tradingAreaIds:*3662* OR
        // :*3664*  OR ....)
        //星级
        if(EmptyUtils.isNotEmpty(searchHotelVO.getHotelLevel())){
            sb.append(" AND hotelLevel:"+searchHotelVO.getHotelLevel());
        }


        //过滤查询 (价格)  minPrice
        if(EmptyUtils.isNotEmpty(searchHotelVO.getMaxPrice()) && EmptyUtils.isNotEmpty(searchHotelVO.getMinPrice())){
            query.setFilterQueries("minPrice:["+searchHotelVO.getMinPrice()+" TO "+searchHotelVO.getMaxPrice()+"]");
        }
        query.setQuery(sb.toString());
        //设置分页条件
        query.setRows(searchHotelVO.getPageSize());
        query.setStart((searchHotelVO.getPageNo()-1) * searchHotelVO.getPageSize());

        QueryResponse queryResponse = null;
        try {
            //执行查询
            queryResponse  = solrServer.query(query);
            SolrDocumentList results = queryResponse.getResults();

            List<ItripHotelVO> beans = queryResponse.getBeans(ItripHotelVO.class);

            Page page = new Page(searchHotelVO.getPageNo(),searchHotelVO.getPageSize(),Integer.valueOf(String.valueOf(results.getNumFound())));
            page.setRows(beans);
            return DtoUtil.returnDataSuccess(page);


        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        return DtoUtil.returnSuccess();
    }

    @PostMapping("/searchItripHotelListByHotCity")
    public Dto remen(@RequestBody SearchHotCityVO searchHotCityVO) {
        //获得连接
        SolrServer solrConnect = SolrUtils.getSolrConnect();
        //用来写查询条件的
        SolrQuery query1 = new SolrQuery();
        SolrDocumentList results = null;

        //设置查询全部数据的条件
        //query.setQuery("*:*");
        query1.setQuery("cityId:"+searchHotCityVO.getCityId());
        query1.setRows(searchHotCityVO.getCount());

        //显示的字段
        query1.setFields("address","avgScore","commentCount","extendPropertyNames","extendPropertyPics","icon-food","featureNames"
                ,"hotelLevel","hotelName","id","imgUrl","isOkCount","maxPrice","minPrice","redundantCityName","redundantCountryName","redundantProvinceName","tradingAreaNames");
        try {
            //执行查询 ,把查询添加放里面  ,获得结果
            QueryResponse query2 = solrConnect.query(query1);
            results = query2.getResults();
            for (SolrDocument result : results) {
                //System.out.println(result);
            }

        } catch (SolrServerException e) {
            e.printStackTrace();
        }finally {
            SolrUtils.solrServerStop(solrConnect);
        }

        return DtoUtil.returnSuccess("获取成功",results);
    }
}
